///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_CUNARCHIVER_H_
#define _BUOLA_IO_CUNARCHIVER_H_

#include <buola/io/cbufferstream.h>
#include <archive.h>

namespace buola { namespace io {

class CUnarchiverStream : public CBufferStream
{
public:
    CUnarchiverStream(::archive *pArchive);
    ~CUnarchiverStream();
    
private:
    void ResetForNextFile();
    
    CResult DoRead(uint8_t *pData, size_t pLen) override;
    CResult DoIgnore(size_t pLen) override;
    CResult DoUnderflow() override;
    CResult DoWrite(const uint8_t *pData, size_t pLen) override;
    void DoFlush() override;
    off_t DoSeek(off_t pPos, ESeek pMode) override;
    off_t DoTell() override;
    
private:
    ::archive *mArchive;
    off_t mNextBlockOffset;
    
    friend class CUnarchiver;
};

typedef intrusive_ptr<CUnarchiverStream> PUnarchiverStream;
    
class CUnarchiver
{
    static const size_t cDefaultBufferLength=8192;

public:
    CUnarchiver(PBufferStream pStream);
    CUnarchiver(const io::CURI &pURI);
    ~CUnarchiver();
    
    bool Next();
    
    std::string GetPath();
    size_t GetFileSize();

    const PUnarchiverStream &GetStream();
    
private:
    static __LA_SSIZE_T ReadCallback(::archive *pArchive,void *pData,const void **pBuffer);
    static __LA_INT64_T SkipCallback(::archive *pArchive,void *pData,__LA_INT64_T pRequest);
    static __LA_INT64_T SeekCallback(::archive *pArchive,void *pData,__LA_INT64_T pRequest,int pWhence);
    
private:    
    PBufferStream mStream;
    PUnarchiverStream mUnarchiverStream;
    ::archive *mArchive;
    ::archive_entry *mEntry;
};

/*namespace io*/ } /*namespace buola*/ }

#endif
